<template>
  <div id="app">
    <div class="login-container">
      <div class="left">
        <div class="title-box">
          <img src="../../assets/img/login_icon.png" alt="" />
          <span class="title">黑马面面</span>
          <span class="line"></span>
          <span class="sub-title">用户登录</span>
        </div>
        <!-- 
          mdoel:绑定模型，他这个里面的属性，根据业务逻辑来
          rules：校验规则
          ref：拿到当前的el-for表单的值
          validate：全局验证
         -->
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          validate
          style="margin-top:29px"
        >
          <!-- 输入手机号码验证 -->
          <el-form-item prop="phone">
            <el-input
              prefix-icon="el-icon-user"
              v-model="ruleForm.phone"
              placeholder="请输入手机号码"
            ></el-input>
          </el-form-item>
          <!-- 
            输入密码验证
            show-password：显示或隐藏密码
           -->
          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-lock"
              v-model="ruleForm.password"
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
          <!-- 输入验证码验证 -->
          <el-row prop="code">
            <el-col :span="16">
              <el-form-item prop="code">
                <el-input
                  prefix-icon="el-icon-key"
                  v-model="ruleForm.code"
                  placeholder="请输入验证码"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <img
                @click="changeLoginCaptcha"
                style="width:100%;height:40px;margin-left:14px"
                :src="loginCaptcha"
                alt=""
              />
            </el-col>
          </el-row>
          <el-form-item prop="checked">
            <el-checkbox v-model="ruleForm.checked"></el-checkbox>
            我已阅读并同意
            <el-link type="primary" href="http://www.baidu.com" target="_blank"
              >用户协议</el-link
            >和
            <el-link type="primary" href="http://www.jd.com" target="_blank"
              >隐私条款</el-link
            >
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width:100%" @click="login"
              >登 录</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              style="width:100%"
              @click="dialogFormVisible"
              >注 册</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="right">
        <img src="../../assets/img/login_bg.png" alt="" />
      </div>
      <!-- 
        ref：绑定，方便后面使用
       -->
      <register ref="register"></register>
    </div>
  </div>
</template>
<script>
// import {saveToken} from '@/utils/token'
// 导入子组件
import register from './Register'
export default {
  // 声明一个变量，方便浏览器测试时调用
  name: 'Login',
  // 注册子组件
  components: {
    register
  },
  data () {
    return {
      loginCaptcha: `${process.env.VUE_APP_BASEURL}/captcha?type=login`,
      ruleForm: {
        phone: '18511111111', // 手机号码
        password: '12345678', //密码
        code: '', //验证码
        checked: true //默认选中
      },

      // 校验规则
      rules: {
        phone: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('手机号码不能为空'))
              }
              const reg = /^1[3456789][0-9]{9}$/
              if (!reg.test(value)) {
                return callback(new Error('手机号不合法'))
              }
              callback()
            },
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: 'blur'
          },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空',
            trigger: 'blur'
          }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              value ? callback() : callback(new Error('必须勾选用户协议'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    // 点击图片发送请求更换验证码
    changeLoginCaptcha () {
      this.loginCaptcha = `${
        process.env.VUE_APP_BASEURL
      }/captcha?type=login&yzm=${new Date() - 0}`
    },
    // 给登录按钮设置点击事件，$refs获取dom的值
    // validate 清空表单的值和规则
    // async和await是成对的 它可以使代码转换成异步来代码执行
    login () {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return
        const res = await this.$axios.post('/login', this.ruleForm)
        if (res.code == 200) {
          this.$message.success('登录成功')
          localStorage.setItem('heimamm-token', res.data.token)
          this.$router.push('../layout/Index.vue')
        } else {
          this.$message({
            message: res.message,
            type: 'error',
            duration: 1000
          })
        }
      })
    },
    // 点击注册按钮显示对话框
    dialogFormVisible(){
      this.$refs.register.dialogVisible=true
    }
  },
  
}
</script>
<style lang="less">
html,
body,
#app {
  height: 100%;
}
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    img {
      width: 22px;
      height: 17px;
      margin-right: 16px;
      margin-bottom: 7px;
    }
    .title {
      font-size: 24px;
      margin-right: 14px;
    }
    .line {
      width: 1px;
      height: 20px;
      background: #c7c7c7;
      margin-right: 12px;
      display: inline-block;
    }
    .sub-title {
      font-size: 24px;
      margin-right: 14px;
    }
  }
}
</style>
